package com.cskaoyan.market.controller;

import com.cskaoyan.market.db.domain.MarketSearchHistory;
import com.cskaoyan.market.service.MarketHistoryService;
import com.cskaoyan.market.service.MarketHistoryServiceImpl;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.ResponseUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author: YaLiFeng
 * @description: TODO
 * @date: 2024/5/8 9:17
 */
@WebServlet("/admin/history/*")
public class AdminHistoryController extends HttpServlet {
    private MarketHistoryService marketSearchHistory=new MarketHistoryServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String requestURI=req.getRequestURI();
        String op=requestURI.replace(req.getContextPath()+"/admin/history/","");
        if("list".equals(op)){
            list(req,resp);
        }

    }

    private void list(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String pageParam=req.getParameter("page");
        String limitParam=req.getParameter("limit");
        String userId=req.getParameter("userId");
        String keywordParam=req.getParameter("keyword");
        String sort=req.getParameter("sort");
        String order=req.getParameter("order");
        Integer page=null;
        Integer limit=null;
        try{
            page=Integer.parseInt(pageParam);
            limit=Integer.parseInt(limitParam);
        }catch (NumberFormatException e){
            Object object= ResponseUtil.badArgument();
            String s= JacksonUtil.getObjectMapper().writeValueAsString(object);
            return;
        }
        List<MarketSearchHistory> marketSearchHistoryList=marketSearchHistory.listHistory(page,limit,userId,keywordParam,sort,order);
        Object o=ResponseUtil.okList(marketSearchHistoryList);
        resp.getWriter().println(JacksonUtil.getObjectMapper().writeValueAsString(o));


    }

}
